
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>

using namespace std;

namespace zhz
{

	class string
	{
	public:
		typedef char *iterator;
		typedef const char *const_iterator;
		iterator begin()
		{

			return _str;
		}

		iterator end()
		{

			return _str + _size;
		}

		const_iterator begin() const
		{

			return _str;
		}

		const_iterator end() const
		{

			return _str + _size;
		}

		string(const char *str = "")
		{
			_capacity = strlen(str);
			_size = _capacity;
			_str = new char[_size + 1];
			strcpy(_str, str);
			cout << "构造" << endl;
		}
		string(const string &s)
		{
			reserve(s._capacity);
			for (auto x : s)
			{
				push_back(x);
			}
			cout << "拷贝构造" << endl;
		}

		string(string &&s)
		{
			swap(s);
			cout << "移动构造" << endl;
		}

		string &operator=(string &&s)
		{

			cout << "移动赋值" << endl;
			if (this != &s)
			{
				swap(s);
			}

			return *this;
		}

		~string()
		{
			if (_str)
			{
				delete[] _str;
				_size = 0;
				_capacity = 0;
			}

			cout << "析构" << endl;
		}

		void reserve(int n)
		{
			if (n > _capacity)
			{
				char *newstr = new char[n + 1];
				if (_str)
				{
					strcpy(newstr, _str);
				}
				else
				{
					newstr[0] = '\0';
				}
				delete[] _str;
				_str = newstr;
				_capacity = n;
			}
		}

		void push_back(char ch)
		{
			if (_size >= _capacity)
			{
				int newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
				reserve(newcapacity);
			}
			_str[_size] = ch;
			_size++;
			_str[_size] = '\0';
		}

		string &operator=(const string &s)
		{
			if (this != &s)
			{
				_str[0] = '\0';
				_size = 0;
				reserve(s._capacity);
				for (auto x : s)
				{
					push_back(x);
				}
			}
			cout << "拷贝赋值" << endl;

			return *this;
		}

		char operator[](int n)
		{
			if (n >= _size)
			{
				return ' ';
			}
			return _str[n];
		}

		size_t size() const
		{

			return _size;
		}

		void swap(string &s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

		const char *c_str()
		{
			return _str;
		}

		string &operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

	private:
		char *_str = nullptr;
		int _size = 0;
		int _capacity = 0;
	};
}

// int main()
//{
//
//	zhz::string s1("aaaa");
//	zhz::string s2 = s1;
//	zhz::string s3;
//	s3 = s1;
//
//	zhz::string s4(zhz::string("dsdsad"));
//	zhz::string s5 = "dsdsd";
//
//	return 0;
// }

zhz::string addStrings(string num1, string num2)
{
	zhz::string str;
	int end1 = num1.size() - 1, end2 = num2.size() - 1;
	// 进位
	int next = 0;
	while (end1 >= 0 || end2 >= 0)
	{
		int val1 = end1 >= 0 ? num1[end1--] - '0' : 0;
		int val2 = end2 >= 0 ? num2[end2--] - '0' : 0;
		int ret = val1 + val2 + next;
		next = ret / 10;
		ret = ret % 10;
		str += ('0' + ret);
	}
	if (next == 1)
		str += '1';
	reverse(str.begin(), str.end());
	return str;
}

// // 场景1
// int main()
// {
// 	zhz::string ret = addStrings("11111", "2222");
// 	cout << (void*)ret.c_str() << endl;
// 	return 0;
// }

// 场景2
int main()
{
	zhz::string ret;
	ret = addStrings("11111", "2222");
	cout << (void*)ret.c_str() << endl;
	return 0;
}